package com.nbcio.demo.controller;

import lombok.RequiredArgsConstructor;
import reactor.core.publisher.Flux;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nbcio.common.core.domain.R;

import com.nbcio.demo.domain.DeepSeekRequest;
import com.nbcio.demo.service.IDeepSeekService;

import io.github.pigmesh.ai.deepseek.core.DeepSeekClient;
import io.github.pigmesh.ai.deepseek.core.chat.ChatCompletionResponse;

/**
 * deepseek api 演示案例
 *
 * @author nbacheng
 */

@RequiredArgsConstructor
@RestController
@RequestMapping("/demo/deepseek")
public class DeepSeekController {

	private final IDeepSeekService deepSeekService;
	private final DeepSeekClient deepSeekClient;

    @PostMapping("/chat")
    public R<?> chat(@RequestBody DeepSeekRequest request) {
        try {
            String response = deepSeekService.callDeepSeekAPI(request);
            return R.ok(response);
        } catch (Exception e) {
            return R.fail("API调用失败: " + e.getMessage());
        }
    }
    
    @GetMapping(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<ChatCompletionResponse> chat(String prompt) {
        return deepSeekClient.chatFluxCompletion(prompt);
    }
    
    /*@PostMapping("/stream")
    public void deepSeekStream(@RequestBody DeepSeekRequest request, HttpServletResponse response) throws IOException {
        // 设置响应头
        response.setContentType("text/event-stream;charset=UTF-8");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Connection", "keep-alive");

        // 调用服务层方法
        deepSeekService.callDeepSeekStreamAPI(request, response);
    }*/
    
}
